//
//  LGSportsEvaSelectCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/4.
//  运动评估--选择Cell

#import "LGSportsEvaSelectCell.h"

#define itemHeight   viewPix(44)

@interface LGSportsEvaSelectCell()
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;

@end

@implementation LGSportsEvaSelectCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.baseView];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(10));
            make.left.equalTo(self.contentView).offset(viewPix(32));
            make.right.equalTo(self.contentView).offset(-viewPix(32));
        }];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(0);
            make.left.right.bottom.equalTo(self.contentView);
            make.height.equalTo(@(itemHeight));
        }];
    }
    return  self;
}

-(void)setResultDic:(NSDictionary *)resultDic{
    _resultDic = resultDic;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    
    for (UIButton *button in self.itemArray) {
        [button removeFromSuperview];
    }
    [self.itemArray removeAllObjects];
    
    NSString *title = LGNSString(dataDic[@"title"]);
    self.titleLabel.text = title;
    CGFloat topMargin = (title.length > 0)?viewPix(38):viewPix(5);
    CGFloat bottom = topMargin+itemHeight;
    NSArray *itemArray = dataDic[@"items"][0][@"options"];
    NSString *key = LGNSString(dataDic[@"items"][0][@"name"]);
    NSInteger lineCount = (itemArray.count>4)?4:itemArray.count;
    CGFloat itemWidth = (Screen_W-viewPix(24))/lineCount;
    for (NSInteger i=0; i<itemArray.count; i++) {
        UIButton *button = [UIButton buttonWithTitle:LGNSString(itemArray[i]) titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        button.frame = CGRectMake(viewPix(16)+itemWidth*(i%lineCount), topMargin+itemHeight*(i/lineCount), itemWidth-viewPix(8), itemHeight-viewPix(12));
        button.cornerRidus = viewPix(16);
        button.tag = 1000+i;
        if([self.resultDic.allKeys containsObject:key] && [self.resultDic[key] isEqualToString:LGNSString(itemArray[i])]){
            button.selected = YES;
        }else{
            button.selected = NO;
        }
        [button setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(itemButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:button];
        [self.itemArray addObject:button];
        if(i == itemArray.count-1){
            bottom = kMaxY(button.frame)+viewPix(12);
        }
    }
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(bottom));
    }];
}

//选中按钮
-(void)itemButtonTouched:(UIButton*)sender{
    NSString *key = LGNSString(self.dataDic[@"items"][0][@"name"]);
    if(sender.selected == YES){
        //取消选择
        sender.selected = !sender.selected;
        if(self.delegate && [self.delegate respondsToSelector:@selector(selectContent:select:key:)]){
            [self.delegate selectContent:sender.titleLabel.text select:sender.selected key:key];
        }
        
    }else{
        //选中
        for (UIButton *button in self.itemArray) {
            button.selected = NO;
        }
        sender.selected = YES;
        if(self.delegate && [self.delegate respondsToSelector:@selector(selectContent:select:key:)]){
            [self.delegate selectContent:sender.titleLabel.text select:sender.selected key:key];
        }
    }
  
    
}

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] init];
    }
    return _baseView;
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
